package com.my.server1.controller;

import com.my.server1.entity.Orders;
import com.my.server1.service.OrdersService;
import com.my.server1.service.TestService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("s1")
public class CustomerController {
    @Autowired
    private TestService testService;

    @Autowired
    private OrdersService ordersService;

    @RequestMapping("get")
    @GlobalTransactional(name = "seata-server", rollbackFor = Exception.class)
    public String get(){
        StringBuilder rs = new StringBuilder("start-->");
        //生成订单，操作orders表
        Orders orders = new Orders();
        orders.setId(UUID.randomUUID().toString());
        orders.setUserId("wang");
        orders.setGoodId("qianBi");
        orders.setBuyNumber(1);
        ordersService.save(orders);
        StringBuilder stringBuilder1 = new StringBuilder("生成订单-->支付成功-->");
        rs.append(stringBuilder1);
        //减少库存，调用微服务server2，操作goods表
        String s = testService.get(orders.getGoodId());
        StringBuilder stringBuilder2 = new StringBuilder(s);
        rs.append(stringBuilder2);
        return rs.toString();
    }
}
